package com.dz.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dz.reggie.dto.DishDto;
import com.dz.reggie.entity.Dish;
import com.dz.reggie.entity.DishFlavor;
import com.dz.reggie.entity.Setmeal;
import com.dz.reggie.entity.SetmealDish;
import com.dz.reggie.mapper.DishMapper;
import com.dz.reggie.service.DishFlavorService;
import com.dz.reggie.service.DishService;
import com.dz.reggie.service.SetmealDishService;
import com.dz.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;

    /**根据id查询菜品基本信息和口味信息
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        DishDto dishDto = new DishDto();
        Dish dish = this.getById(id);
        BeanUtils.copyProperties(dish, dishDto);

        // 查询口味--集合,需要构造条件查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(list);

        return dishDto;
    }

    /**
     * 更新dish及其口味信息
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        // 更新dish表
        this.updateById(dishDto);
        // 清理口味信息数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        // 添加新的口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) ->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 新增菜品并保存对应的口味信息
     * @param dishDto
     */
    @Override
    @Transactional  // 多张表操作，需要加入事务注解，并在启动类中开启
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);
        // 得到菜品id
        Long dishId = dishDto.getId();
        // 为该菜品口味绑定菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();
        // 使用stream流赋值
        flavors.stream().map((item) ->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    @Transactional  // 多张表操作，需要加入事务注解，并在启动类中开启
    public void deleteByIdsWithFlacor(ArrayList<Long> idList) {
        // 1 菜品口味表中根据dish_id批量删除 -- 待优化
        HashMap<String, Object> map = new HashMap<>();
        // todo - 改善 如何批量删除，以非主键字段为条件的表中数据
        for (Long id : idList){
            map.put("dish_id", id);
            dishFlavorService.removeByMap(map);
            map.clear();
        }
        // 2  删除菜品数据
       super.removeByIds(idList);
    }

    /**
     * 启售：将菜品及关联的套餐启售
     * @param idList
     */
    @Override
    public void openDishStatus(ArrayList<Long> idList) {
        List<Dish> dishList = dishService.listByIds(idList);

        for (Dish dish : dishList){
            dish.setStatus(1);
            // 所关联的套餐也改为启售状态
            LambdaQueryWrapper<SetmealDish> setmealDishQW = new LambdaQueryWrapper();
            setmealDishQW.eq(SetmealDish::getDishId, dish.getId());
            SetmealDish setmealDish = setmealDishService.getOne(setmealDishQW);
            if (setmealDish == null)
                continue;
            Long setmealId = setmealDish.getSetmealId();
            Setmeal setmeal = setmealService.getById(setmealId);
            setmeal.setStatus(1);
            setmealService.updateById(setmeal);
        }
        // 更改数据库中数据
        dishService.updateBatchById(dishList);


    }

    @Override
    public void closeDishStatus(ArrayList<Long> idList) {
        List<Dish> dishList = dishService.listByIds(idList);

        for (Dish dish : dishList){
            dish.setStatus(0);
            // 所关联的套餐也改为启售状态
            LambdaQueryWrapper<SetmealDish> setmealDishQW = new LambdaQueryWrapper();
            setmealDishQW.eq(SetmealDish::getDishId, dish.getId());
            SetmealDish setmealDish = setmealDishService.getOne(setmealDishQW);
            if (setmealDish == null)
                continue;
            Long setmealId = setmealDish.getSetmealId();
            Setmeal setmeal = setmealService.getById(setmealId);
            setmeal.setStatus(0);
            setmealService.updateById(setmeal);
        }
        // 更改数据库中数据
        dishService.updateBatchById(dishList);
    }
}
